﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hiro.Interfaces;

namespace Hiro
{
    /// <summary>
    /// A class that defines a set of extension methods for the <see cref="IService"/> interface.
    /// </summary>
    internal static class ServiceExtensions
    {
        /// <summary>
        /// Groups a list of services by type.
        /// </summary>
        /// <param name="services">The services that need to be grouped into types.</param>
        /// <returns>A set of services grouped by type.</returns>
        public static Dictionary<Type, List<IService>> GroupByType(this IEnumerable<IService> services)
        {
            // Group the services by type
            var serviceMap = new Dictionary<Type, List<IService>>();
            foreach (var service in services)
            {
                var dependency = service.TargetDependency;
                var serviceType = dependency.ServiceType;
                if (!serviceMap.ContainsKey(serviceType))
                    serviceMap[serviceType] = new List<IService>();

                serviceMap[serviceType].Add(service);
            }

            return serviceMap;
        }
    }
}
